﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DAL;
using DAL.Models;
using BAL;
using BAL.Interfaces;
using BibliotecaVirtual.Models;

namespace BibliotecaVirtual.Controllers
{ 
    public class SociosController : Controller
    {
        private ISociosService BALSocios = null;
        private IUsuarioSocioService BALUsuarioSocio = null;

        public SociosController(ISociosService SociosService, IUsuarioSocioService UsuarioSocioService)
        {
            BALSocios = SociosService;
            BALUsuarioSocio = UsuarioSocioService;
        }

        //
        // GET: /Socios/
        [Authorize(Roles = Libreria.Constantes.SOCIO)]
        public ViewResult Index()
        {
            List<Socios> socios = new List<Socios>();
            socios.Add(BALUsuarioSocio.GetSocioByGuid((Guid)Membership.GetUser().ProviderUserKey));
            if (System.Web.Security.Roles.GetRolesForUser().Contains(Libreria.Constantes.ADMINISTRADOR))
            {
                socios = BALSocios.GetAllValidados().ToList<Socios>();
            }
            return View(socios);
        }

        //
        // GET: /Socios/Details/5
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ViewResult Details(long id)
        {
            object[] keys = new object[1] { id };
            return View(BALSocios.Find(keys));
        }

        //
        // GET: /Socios/Create
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Socios/Create

        [HttpPost]
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Create(Socios item)
        {
            if (ModelState.IsValid)
            {
                BALSocios.Create(item);
                return RedirectToAction("Index");  
            }

            return View(item);
        }
        
        //
        // GET: /Socios/Edit/5
 [Authorize(Roles = Libreria.Constantes.SOCIO)]
        public ActionResult Edit(long id)
        {
            object[] keys = new object[1] { id };
            return View(BALSocios.Find(keys));
        }

        //
        // POST: /Socios/Edit/5

        [HttpPost]
        [Authorize(Roles = Libreria.Constantes.SOCIO)]
        public ActionResult Edit(Socios item)
        {
            if (ModelState.IsValid)
            {
                BALSocios.Edit(item);
                return RedirectToAction("Index");
            }
            return View(item);
        }

        //
        // GET: /Socios/Delete/5
 [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        public ActionResult Delete(long id)
        {
            object[] keys = new object[1] { id };
            return View(BALSocios.Find(keys));
        }

        //
        // POST: /Socios/Delete/5
        [Authorize(Roles = Libreria.Constantes.ADMINISTRADOR)]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            object[] keys = new object[1] { id };
            Socios item = BALSocios.Find(keys);
            BALSocios.Remove(item);
            return RedirectToAction("Index");
        }

        //protected override void Dispose(bool disposing)
        //{
        //    BALSocios.Dispose();
        //    base.Dispose(disposing);
        //}

    }
}